package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author kabuda
 * @create 2022-09-07 20:01
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //购物车增加菜品/套餐数量
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.get();
        shoppingCart.setUserId(userId);
        //判断该菜品或者套餐已经添加到购物车
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            //添加的是菜品类
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加的是套餐类
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if(one!=null){
            //如果已经添加，则number+1（此处不考虑口味问题）
            shoppingCart.setNumber(one.getNumber()+1);
            shoppingCart.setId(one.getId());
            shoppingCartService.updateById(shoppingCart);
        }else{
            shoppingCart.setCreateTime(LocalDateTime.now());
            //如果没添加，则添加，默认number为1
            shoppingCartService.save(shoppingCart);

        }

        return R.success(shoppingCart);
    }
    //展示购物车列表
    @RequestMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.get());
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    //清空购物策划
    @RequestMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.get());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功！");
    }
    //购物车减少菜品/套餐数量
    @RequestMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.get();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            //添加的是菜品类
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加的是套餐类
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        one.setNumber(one.getNumber()-1);
        one.setId(one.getId());
        shoppingCartService.updateById(one);

        return R.success(one);
    }




}
